package com.vilen.weixin.kit;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2016/2/17.
 */
public class WeixinMessageKit {
    public static String handlerReceiveMsg(HttpServletRequest request) throws IOException {
        Map<String,String> msgMap = reqMsg2Map(request);
        System.out.println(msgMap);
        //查询重复
        if(!DuplicateMessageKit.checkDuplicate(msgMap)){
            String rel = DuplicateMessageKit.getRel(msgMap);
            return rel;
        }
        String msgType = msgMap.get("MsgType");
        if("event".equals(msgType.trim())) {
            String rel = WeixinEventKit.handlerMsg(msgMap);
            DuplicateMessageKit.setRel(msgMap,rel);
            //事件消息
            return rel;
        }else {
            //处理文本消息
            return handlerCommonMsg(msgMap);
        }
    }

    private static String handlerCommonMsg(Map<String, String> msgMap) {
        return null;
    }

    private static Map<String,String> reqMsg2Map(HttpServletRequest request)throws IOException {
        String xml = req2xml(request);
        try {
            Map<String,String> maps = new HashMap<String, String>();
            Document document = DocumentHelper.parseText(xml);
            Element root = document.getRootElement();
            List<Element> eles = root.elements();
            for (Element e: eles) {
                maps.put(e.getName(),e.getTextTrim());
            }
            return maps;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static String req2xml (HttpServletRequest request) throws IOException {
        BufferedReader br = null;
        br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String str = null;
        StringBuffer sb = new StringBuffer();
        while((str=br.readLine())!=null) {
            sb.append(str);
        }
        return sb.toString();
    }
    public static String map2xml(Map<String, Object> map) throws IOException {
        Document d = DocumentHelper.createDocument();
        Element root = d.addElement("xml");
        Set<String> keys = map.keySet();
        for (String key: keys) {
            Object o = map.get(key);
            if(o instanceof String) {
                String t = (String)o;
                if(t.indexOf("<a")>=0) {
                    root.addElement(key).addCDATA(t);
                }else {
                    root.addElement(key).addText((String) o);
                }
            }else {
                //其他类型
            }
        }
        StringWriter sw = new StringWriter();
        //d.write(sw); &lt 格式不对
        XMLWriter xw = new XMLWriter(sw);
        xw.setEscapeText(false);
        xw.write(d);
        return sw.toString();
    }

}
